#include "stdafx.h"
#include "provider.h"
#include "lookup.h"
#include "config.h"
#include "resource.h"
#include "ui_main_conf.h"
#include "menu.h"


contextmenu_item_factory_t<lyric_grabber_context_menu> g_context;


GUID lyric_grabber_context_menu::get_item_guid(unsigned p_index)
{
	// {0F5308BD-EFE5-428b-8702-E470B65172D2}
	static const GUID conf_guid = 
	{ 0xf5308bd, 0xefe5, 0x428b, { 0x87, 0x2, 0xe4, 0x70, 0xb6, 0x51, 0x72, 0xd2 } };

	if (p_index < get_num_items() - 1)
		return g_lyric_provider_list[p_index]->get_context_guid();
	else
		return conf_guid;
}


bool lyric_grabber_context_menu::get_item_description(unsigned p_index,pfc::string_base & p_out)
{
	if (p_index < get_num_items() - 1)
		return g_lyric_provider_list[p_index]->get_description(p_out);
	else
		return false;
}

void lyric_grabber_context_menu::get_item_name(unsigned p_index, pfc::string_base & p_out)
{
	if (p_index < get_num_items() - 1)
		return g_lyric_provider_list[p_index]->get_name(p_out);
	else
		p_out = "Configuration";
}

unsigned lyric_grabber_context_menu::get_num_items()
{
	return g_lyric_provider_list.get_count() + 1;
}

void lyric_grabber_context_menu::context_command(unsigned p_index, metadb_handle_list_cref p_data,const GUID& p_caller)
{
	if (p_index < get_num_items() -1)
	{
		service_ptr_t<lyric_lookup_task> task = new service_impl_t<lyric_lookup_task>(p_data);
		task->set_provider(g_lyric_provider_list[p_index]);
		task->start();
	}
	else
	{
		if (!m_conf_dlg || !m_conf_dlg->IsWindow())
		{
			m_conf_dlg = new CMainConf;
			m_conf_dlg->Create(core_api::get_main_window());
		}

		m_conf_dlg->CenterWindow(core_api::get_main_window());
		m_conf_dlg->ShowWindow(SW_SHOW);
	}
}

bool lyric_grabber_context_menu::context_get_display(unsigned p_index,metadb_handle_list_cref p_data,pfc::string_base & p_out,unsigned & p_displayflags,const GUID & p_caller)
{
	get_item_name(p_index,p_out);

	if (p_index == get_num_items() - 1 && m_conf_dlg && m_conf_dlg->IsWindow())
	{
		p_displayflags = FLAG_GRAYED;
	}

	return true;
}
